package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.*;
import com.ysd.lis.mapper.sys.SysModuleMapper;
import com.ysd.lis.mapper.sys.SysPointsMapper;
import com.ysd.lis.service.SysPointsService;
import com.ysd.lis.vo.ModuleAndPointTreeVo;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * cold_chain.t_role 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2023-11-08
 */
@Service
public class SysPointsServiceImpl extends ServiceImpl<SysPointsMapper, SysPoints> implements SysPointsService {

    @Autowired
    SysPointsMapper sysPointsMapper;
    @Autowired
    SysModuleMapper moduleMapper;

    public Result addPoints(SysPoints sysPoints) {
        Map returnMap = new HashMap<>();
        SysUser sysUser = RedisUserManager.getUser();
        if (null != sysUser) {
            if (StringUtils.isNotBlank(sysUser.getOrgId())) {
                sysPoints.setOrgId(sysUser.getOrgId());
            }
        }
        sysPointsMapper.insert(sysPoints);
        String id = sysPoints.getId();
        returnMap.put("healthCheckId", id);
        return Result.succ(1, "添加功能点成功", returnMap);
    }

    public Result editPoints(SysPoints sysPoints) {
        Map returnMap = new HashMap<>();
        this.baseMapper.updateById(sysPoints);
        returnMap.put("points", sysPoints);
        return Result.succ(1, "修改功能点成功", returnMap);
    }

    public Result deletePoints(String id) {
        SysPoints sysPoints = sysPointsMapper.selectById(id);
        sysPoints.setDelFlag(1);
        int deleteById = sysPointsMapper.updateById(sysPoints);
        if (deleteById > 0) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("moduleId", id);
            return Result.succ(1, "删除成功", resultMap);
        } else {
            return Result.fail("删除失败");
        }

    }

    @Override
    public Result getModuleAndPointList() {

        LambdaQueryWrapper<SysModule> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SysModule::getDelFlag, 0);
        List<SysModule> modules = moduleMapper.selectList(lambdaQueryWrapper);
        LambdaQueryWrapper<SysPoints> pointsLambdaQueryWrapper = new LambdaQueryWrapper();
        pointsLambdaQueryWrapper.eq(SysPoints::getDelFlag, 0);
        List<SysPoints> pointsList = sysPointsMapper.selectList(pointsLambdaQueryWrapper);

        Map<String, ModuleAndPointTreeVo> moduleMap = new HashMap<>();
        if (ToolsUtils.isNotEmpty(modules)) {
            // 遍历 modules 列表，将 SysModule 对象转换为 ModuleAndPointTreeVo 对象，并放入 moduleMap 中
            for (SysModule module : modules) {
                ModuleAndPointTreeVo moduleAndPointTreeVo = new ModuleAndPointTreeVo();
                moduleAndPointTreeVo.setId(module.getId());
                moduleAndPointTreeVo.setTitle(module.getName());
                moduleAndPointTreeVo.setIcon(module.getIcon());
                moduleAndPointTreeVo.setChildren(new ArrayList<>());
                moduleMap.put(module.getId(), moduleAndPointTreeVo);
            }

            // 遍历 pointsList 列表，将 SysPoints 对象转换为 ModuleAndPointTreeVo 对象，并添加到对应的父节点下
            if (ToolsUtils.isNotEmpty(pointsList)) {
                for (SysPoints point : pointsList) {
                    ModuleAndPointTreeVo pointVo = new ModuleAndPointTreeVo();
                    pointVo.setId(point.getId());
                    pointVo.setTitle(point.getTitle());
                    pointVo.setIcon(point.getIcon());
                    ModuleAndPointTreeVo parentModule = moduleMap.get(point.getModuleId());
                    if (parentModule != null) {
                        if (parentModule.getChildren() == null) {
                            parentModule.setChildren(new ArrayList<>());
                        }
                        parentModule.getChildren().add(pointVo);
                    }
                }
            }

        }
        List<ModuleAndPointTreeVo> ListTree = new ArrayList<>(moduleMap.values());
        return Result.succ(ListTree);
    }


//    public Result pointsPageList(PointsPageParam pointsPageParam) {
//        Map map = new HashMap();
//        List<Map> sysPointsList = this.sysPointsMapper.getPointsList(pointsPageParam);
//        List<Map> returnList = new ArrayList<>();
//        sysPointsList.forEach(a -> {
//            if (StringUtils.isBlank((String) a.get("parent_id"))) {
//                a.put("children", this.getChildren(a, sysPointsList));
//                returnList.add(a);
//            }
//        });
//        map.put("menuList", returnList);
//        return Result.succ(1, "成功", map);
//    }
//
//    //遍历所有的子集功能点
//    public List<Map> getChildren(Map a, List<Map> sysPointsList) {
//        List<Map> result = new ArrayList();
//        for (Map menu : sysPointsList) {
//            if (StringUtils.isNotBlank((String) menu.get("parent_id")) && menu.get("parent_id").equals(a.get("id"))) {
//                result.add(menu);
//            }
//        }
//        for (Map menu : result) {
//            menu.put("children", getChildren(menu, sysPointsList));
//        }
//        if (result.size() == 0) {
//            return new ArrayList<>();
//        }
//        return result;
//
//    }

    public List<SysPoints> getSysPointsByRoleId(String roleId) {
        MPJLambdaWrapper<SysPoints> pointByRoleIdWrapper = new MPJLambdaWrapper<>();
        pointByRoleIdWrapper
                .distinct()
                .selectAll(SysPoints.class)
                .leftJoin(SysRolePoints.class, SysRolePoints::getPointsId, SysPoints::getId)
                .eq(SysPoints::getDelFlag, "0")
                .eq(SysRolePoints::getRoleId, roleId);
        List<SysPoints> sysPoints = sysPointsMapper.selectList(pointByRoleIdWrapper);
        return sysPoints;
    }


}
